package Model;

import Entity.Account;
import Entity.AccountDetails;
import Entity.Duration;
import Entity.LoanType;
import Manager.Manager;
import java.text.SimpleDateFormat;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author CONG
 */
public class CustomerDurationsTableModel extends AbstractTableModel {

    private Manager manager;

    public CustomerDurationsTableModel(Manager manager) {
        this.manager = manager;
    }

    @Override
    public int getRowCount() {
        return manager.getCustomerDurationsManager().getSize();
    }

    @Override
    public int getColumnCount() {
        return 9;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Account acc = manager.getCustomerDurationsManager().getAcc(rowIndex);
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
        int registerNo = acc.getResgisterNo();
        AccountDetails accde = null;
        Duration dur = null;
        LoanType loanType = null;
        for (int i = 0; i < manager.getAccountDetailsManager().getSize(); i++) {
            if (manager.getAccountDetailsManager().getAccountDetails(i).getRegisterNo() == registerNo) {
                accde = manager.getAccountDetailsManager().getAccountDetails(i);
            }
        }
        if (accde != null) {
            int accountLoanCode = accde.getAccountLoanCode();
            for (int i = 0; i < manager.getDurationManager().getSize(); i++) {
                if (manager.getDurationManager().getDuration(i).getAccountLoanCode() == accountLoanCode) {
                    dur = manager.getDurationManager().getDuration(i);
                }
            }
        }
        if (dur != null) {
            int loanTypeCode = dur.getLoanTypeCode();
            for (int i = 0; i < manager.getLoanTypeManager().getSize(); i++) {
                if (manager.getLoanTypeManager().getLoanType(i).getLoanTypeCode() == loanTypeCode) {
                    loanType = manager.getLoanTypeManager().getLoanType(i);
                }
            }
        }
        switch (columnIndex) {
            case 0:
                if (dur != null) {
                    return dur.getAccountLoanCode();
                } else {
                    return "No";
                }
            case 1:
                if(loanType!=null)return loanType.getName();
                else return "No";
            case 2:
                if(loanType!=null)return loanType.getInterestRate();
                else return "No";
            case 3:
                if (dur != null) {
                    return dur.getAmountLoan();
                } else {
                    return "No";
                }
            case 4:
                if(accde!=null)return accde.getPeriod();
                else return "No";
            case 5:
                if (dur != null) {
                    String startDate = dateFormat.format(dur.getStartDate());
                    return startDate;
                } else {
                    return "No";
                }
            case 6:
                if(dur!=null){
                  String endDate = dateFormat.format(dur.getEndDate());
                return endDate;
                }else return "No";
            case 7:
                if(dur!=null)return dur.getBalance();
                else return "No";
            case 8:
                if(dur!=null)return dur.isStatus();
                else return false;
            default:
                return null;
        }
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "Loan ID";
            case 1:
                return "Loan Type";
            case 2:
                return "Rate(%)";
            case 3:
                return "Loan Amount";
            case 4:
                return "Period Year";
            case 5:
                return "Start Date";
            case 6:
                return "End Date";
            case 7:
                return "Balance";
            case 8:
                return "Loan Status";
            default:
                return null;
        }
    }

    public void addAcc(Account acc) {
        manager.getCustomerDurationsManager().addAcc(acc);
    }

    public int getSize() {
        return manager.getCustomerDurationsManager().getSize();
    }
    public void removeRow(int index){
        manager.getCustomerDurationsManager().removeAcc(index);
        fireTableDataChanged();
    }
    public Account getAcc(int index){
        return manager.getAccountManager().getAccount(index);
    }
}
